﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TLKD06
    {

        public TLKD06()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VKUNDVER();
            DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";

                ZAHLBED = "";
            }
            else
            {
                KEY = TMV.KDVKUNDNR + TMV.KDVVB + TMV.KDVVG;
                ZAHLBED = TMV.KDVZAHLBED;
            }
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData);
                LFD = VSDDAT.SD22.GETNEXT("0022", "KV", KUNDNR, 1);
                if (LFD == "000001")
                {
                    LFD = VSDDAT.SD22.GETNEXT("0022", "KV", KUNDNR, 1);
                }
                dwData["KDVKUNDNR"] = KUNDNR;
                dwData["KDVVB"] = VB;
                dwData["KDVVG"] = Helpers.Helper.Right(LFD, 3);
                TMV.DKDV01(dwData);
                TMV.InsertKUNDVER(dwData, app.company);
                TBMain.Rows.InsertAt(dwData, 0);
                vwMaster.View.MoveCurrentToFirst();
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
           
            base.SaveCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            LFD = VSDDAT.SD22.GETNEXT("0022", "KV", KUNDNR, 1);
            LFD = VSDDAT.SD22.GETNEXT("0022", "KV", KUNDNR, 1);
            if (LFD == "000001")
            {
                LFD = VSDDAT.SD22.GETNEXT("0022", "KV", KUNDNR, 1);
            }
            dwData["KDVVG"] = Helpers.Helper.Right(LFD, 3);
            TMV.DKDV01(dwData);
            TMV.InsertKUNDVER(dwData, app.company);
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            IsSaved = false;
            base.EditCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DKDV01(dwData);
            string filter = "KDVVB='" + dwMaster["KDVVB"].ToString() + "' AND KDVVG='" + dwMaster["KDVVG"].ToString()+"'";
            DataRow[] rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }
        public override void LOADBYAUF()
        {
            if (!IsSaved)
                return;
            if (ORDERNUM.Length < 14)
                return;
            if (ORDERNUM != KEY)
                return;
            dwData = TBMain.NewRow();
            dwData["KDVMANDANT"] = app.Mandant;
            dwData["KDVKUNDNR"] = " AND KDVKUNDNR='" + ORDERNUM.Substring(0, 8) + "'";
            dwData["KDVVB"] = " AND KDVVB='" + ORDERNUM.Substring(8, 3) + "'";
            dwData["KDVVG"] = " AND KDVVG='" + ORDERNUM.Substring(11, 3) + "'";
            SResult = TMV.QKDV04(dwData);
            DataTable tbTemp = TBMain.Clone();
            tbTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in tbTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            base.LOADBYAUF();
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Trim().Length < 8)
            { //00000000
                return;
            }
            dwData = TBMain.NewRow();
            dwData["KDVMANDANT"] = app.Mandant;
            dwData["KDVKUNDNR"] = " AND KDVKUNDNR='" + EXAUF.Substring(0, 8) + "'";
            SResult = TMV.QKDV04(dwData);
            MergeData(SResult, true);
            base.OnEXAUFChg(vlaue);
        }
        
    }
}
